package com.nkym.client;/*
 * @ClassName LapiClient
 * @Desc TODO
 * @Author 19637
 * @Date 2023/4/20 15:32
 * @Version 1.0
 */

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;


public class LApiClient {
    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    private String accessKey;
    private String secretKey;

    public LApiClient() {
    }

    public LApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public Map<String, String> signatureGenerate() {
        Map<String, String> map = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        String nonce = RandomUtil.randomString(6);
        String sign = accessKey + secretKey + nonce + timestamp;
        //加密
        sign = DigestUtil.md5Hex(sign);
        map.put("sign", sign);
        map.put("nonce", nonce);
        map.put("timestamp", String.valueOf(timestamp));
        map.put("accessKey", accessKey);
        return map;
    }


    public String get(String url, Map<String, Object> params) {
        try {
            Map<String, String> headers = signatureGenerate();
            String jsonStr = JSONUtil.toJsonStr(params);
            String body = HttpRequest.get(url)
                    .body(jsonStr)
                    .addHeaders(headers)
                    .execute()
                    .body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return "服务错误，接口调用失败";
        }
    }
    public String get(String url,String jsonParam) {
        try {
            Map<String, String> headers = signatureGenerate();
            String body = HttpRequest.get(url)
                    .body(jsonParam)
                    .addHeaders(headers)
                    .execute()
                    .body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return "服务错误，接口调用失败";
        }
    }
    public String put(String url,String jsonParam) {
        try {
            Map<String, String> headers = signatureGenerate();

            String body = HttpRequest.put(url)
                    .body(jsonParam)
                    .addHeaders(headers)
                    .execute()
                    .body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return "服务错误，接口调用失败";
        }
    }  public String put(String url, Map<String, Object> params) {
        try {
            Map<String, String> headers = signatureGenerate();
            String jsonStr = JSONUtil.toJsonStr(params);
            String body = HttpRequest.put(url)
                    .body(jsonStr)
                    .addHeaders(headers)
                    .execute()
                    .body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return "服务错误，接口调用失败";
        }
    }
    public String post(String url,String jsonParam) {
        try {
            Map<String, String> headers = signatureGenerate();
            String body = HttpRequest.post(url)
                    .body(jsonParam)
                    .addHeaders(headers)
                    .execute()
                    .body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return "服务错误，接口调用失败";
        }
    }  public String post(String url, Map<String, Object> params) {
        try {
            Map<String, String> headers = signatureGenerate();
            String jsonStr = JSONUtil.toJsonStr(params);
            String body = HttpRequest.post(url)
                    .body(jsonStr)
                    .addHeaders(headers)
                    .execute()
                    .body();
            return body;
        } catch (Exception e) {
            e.printStackTrace();
            return "服务错误，接口调用失败";
        }
    }

}
